<?php
/**
 * Created by PhpStorm.
 * User: zhangjian37
 * Date: 2016/9/25
 * Time: 20:27
 */

$RootDir = $_SERVER['DOCUMENT_ROOT'];
include $RootDir.'/utils/mysql/MysqlUtils.php';

include 'Juhuansuan.php';

$people_num=$_GET['people_num'];
$max_price=$_GET['max_price'];
$select_time=$_GET['select_time'];
$brief=$_GET['brief'];

date_default_timezone_set('PRC');

if($people_num == null){
    $people_num = 200;
}
if($max_price == null){
    $max_price = 2;
}
if($select_time == null){
    $select_time = 10;
}

function getData($people_num=200,$max_price=200){
    $dataArray = array('T'=>'Juhuasuan');
    $conn = conn_mysql("youhuikuaibao");
    $query = "select url,name,pic,people_num,min_price,max_price,actPrice,origPrice from juhuasuan where people_num < ".$people_num." and max_price >= ".(100*$max_price)." order by people_num";

    #echo $query;

    $result = queryRecord($query,$conn);

    while ($row=mysql_fetch_array($result, MYSQL_ASSOC)) {
        $juhuasuan = new Juhuasuan();
        $juhuasuan->url = $row['url'];
        $juhuasuan->name = $row['name'];
        $juhuasuan->pic = $row['pic'];
        $juhuasuan->reminNum = $row['people_num'];
        $juhuasuan->minPrice = $row['min_price'];
        $juhuasuan->maxPrice = $row['max_price'];
        $juhuasuan->actPrice = $row['actPrice'];
        $juhuasuan->origPrice = $row['origPrice'];
        $dataArray[]=$juhuasuan;
    }
    return $dataArray;
}

$dataArray = getData($people_num,$max_price);

$count = count($dataArray);

$music="";
if($count>1&&$brief!=1)$music="299.mp3";



#http://localhost:8081/jhsShow.php?people_num=2000&select_time=10000&max_price=200

$html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>';
$html .= '<head><meta charset="utf-8"><title>jhs</title></head><link href="css/style.css" rel="stylesheet" type="text/css">';
$html .= '<body>';
$html .= '<script type="text/javascript">';
$html .= 'function condition_submit(){';
$html .= '  var select_time = document.getElementById("select_time");';
$html .= '  var people_num = document.getElementById("people_num");';
$html .= '  var max_price = document.getElementById("max_price");';
$html .= '  if (people_num.value != ""){';
$html .= '      document.getElementById("h_people_num").value = people_num.value;';
$html .= '  }';
$html .= '  if (max_price.value != ""){';
$html .= '      document.getElementById("h_max_price").value = max_price.value;';
$html .= '  }';
$html .= '  if (select_time.value != ""){';
$html .= '      document.getElementById("h_select_time").value = select_time.value;';
$html .= '  }';
$html .= '  flush_page();';
$html .= '}';
$html .= 'function getNowFormatDate() {';
$html .= '    var date = new Date();';
$html .= '    var seperator1 = "-";';
$html .= '    var seperator2 = ":";';
$html .= '    var month = date.getMonth() + 1;';
$html .= '    var strDate = date.getDate();';
$html .= '    if (month >= 0 && month <= 9) {';
$html .= '        month = "0" + month;';
$html .= '    }';
$html .= '    if (strDate >= 0 && strDate <= 9) {';
$html .= '        strDate = "0" + strDate;';
$html .= '    }';
$html .= '    var hour = date.getHours();';
$html .= '    var mi = date.getMinutes();';
$html .= '    var ss = date.getSeconds();';
$html .= '    if (hour >= 0 && hour <= 9) {';
$html .= '        hour = "0" + hour;';
$html .= '    }';
$html .= '    if (mi >= 0 && mi <= 9) {';
$html .= '        mi = "0" + mi;';
$html .= '    }';
$html .= '    if (ss >= 0 && ss <= 9) {';
$html .= '        ss = "0" + ss;';
$html .= '    }';
$html .= '    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate';
$html .= '        + " " + hour + seperator2 + mi + seperator2 + ss;';
$html .= '    return currentdate;';
$html .= '}';
$html .= 'var index = 1;';
$html .= 'function flush_page(){';
$html .= '  var divs = document.getElementsByTagName("div");';
$html .= '  for(var i=0;i<divs.length;i++){';
$html .= '      if(divs[i].className == "flush_time"){';
$html .= '          divs[i].innerHTML = "当前时间："+getNowFormatDate();';
$html .= '      }';
$html .= '  }';
$html .= '  if (index == '.$select_time.'){';
$html .= '      var select_time = document.getElementById("h_select_time");';
$html .= '      var people_num = document.getElementById("h_people_num");';
$html .= '      var max_price = document.getElementById("h_max_price");';
$html .= '      var brief = document.getElementById("brief");';
$html .= "      window.location.href='jhsShow.php?people_num='+people_num.value+'&select_time='+select_time.value+'&max_price='+max_price.value+'&brief='+brief.value";
$html .= '  }';
$html .= '  index++;';
$html .= '  setTimeout("flush_page()", 1000);';
$html .= '}';
$html .= 'function notify(title, content,url) {';
$html .= '    if(!title && !content && !url){';
$html .= '        title = "桌面提醒";';
$html .= '        content = "您看到此条信息桌面提醒设置成功";';
$html .= '        url = "/images/guide2.png";';
$html .= '    }';
$html .= '    if (window.webkitNotifications) {';
$html .= '        if (window.webkitNotifications.checkPermission() == 0) { ';
$html .= '            var notif = window.webkitNotifications.createNotification(url, title, content);';
$html .= '            notif.show();  ';
$html .= '         } else {';
$html .= '            window.webkitNotifications.requestPermission($jy.notify);';
$html .= '         }';
$html .= '    }';
$html .= '    else if("Notification" in window){';
$html .= '        if (Notification.permission === "granted") {';
$html .= '            var notification = new Notification(title, {';
$html .= '                "icon": url,  ';
$html .= '                "body": content,  ';
$html .= '            });  ';
$html .= '        }';
$html .= '        else if (Notification.permission !== "denied") {';
$html .= '            Notification.requestPermission(function(permission) {';
$html .= '                if (!("permission" in Notification)) {';
$html .= '                    Notification.permission = permission;';
$html .= '                }  ';
$html .= '                if (permission === "granted") {';
$html .= '                    var notification = new Notification(title, {';
$html .= '                        "icon": url,  ';
$html .= '                        "body": content,  ';
$html .= '                    });  ';
$html .= '                }  ';
$html .= '            });';
$html .= '        }';
$html .= '    }  ';
$html .= '}';
$html .= 'if ('.$count.'>1){';
$html .= '  notify("'.$dataArray[0]->name.'","'.($dataArray[0]->maxPrice/100).'元---'.$dataArray[0]->reminNum.'人'.'","'.$dataArray[0]->pic.'");';
$html .= '}';

$html .= 'setTimeout("flush_page()", 1000);';
$html .= '</script>';
$html .= '<input type="hidden" id="h_people_num" value="'.$people_num.'">';
$html .= '<input type="hidden" id="h_max_price" value="'.$max_price.'">';
$html .= '<input type="hidden" id="h_select_time" value="'.$select_time.'">';
$html .= '<embed autostart="true" loop="true" hidden="true" src="'.$music.'">';
$html .= '<div class="header_line">';
$html .= '    <div class="condition_text">用户名:</div><div class="header_line_text">xxx</div>';
$html .= '    <div class="condition_text">用户组:</div><div class="header_line_text">包月vip</div>';
$html .= '    <div class="condition_text">有效时间:</div><div class="header_line_text">20161010</div>';
$html .= '    <input class="register_input" type="button" value="续费">';
$html .= '    <input class="register_input" type="button" value="退出">';
$html .= '</div>';
$html .= '<div class="navi"><div class="navi_ver_center">福利社红包</div><div class="line"/></div>';
$html .= '<div class="content">';
$html .= '<form action="condition_submit">';
$html .= '    <div class="condition">';
$html .= '        <div class="condition_pn">';
$html .= '          <div class="condition_text">人数<=</div>';
$html .= '          <div class="condition_input"><input type="text" id="people_num" value="'.$people_num.'" style="width:100px"/></div>';
$html .= '          <div class="condition_text">人</div>';
$html .= '        </div>';
$html .= '       &nbsp;';
$html .= '        <div class="condition_pn">';
$html .= '          <div class="condition_text">红包>=</div>';
$html .= '          <div class="condition_input"><input type="text" id="max_price" value="'.$max_price.'" style="width:100px"/></div>';
$html .= '          <div class="condition_text">元</div>';
$html .= '        </div>';
$html .= '        &nbsp;';
$html .= '        刷新间隔:';
$html .= '        <select name="select_time" id="select_time" class="condition_select">';
$html .= '            <option value="10" selected="true">10</option>';
$html .= '            <option value="20">20</option>';
$html .= '            <option value="30">30</option>';
$html .= '        </select>';
$html .= '        <input type="checkbox" id="brief" value="0"/>提示';
$html .= '        <input type="button" onclick="condition_submit()" value="开始">';
$html .= '    </div>';
$html .= '    <div class="condition">';
$html .= '        <div class="condition_pn">';
$html .= '          <div class="condition_text">更新时间：'.date("Y-m-d H:i:s",time()).'</div>';;
$html .= '        </div>';
$html .= '       &nbsp;';
$html .= '        <div class="condition_pn">';
$html .= '          <div class="flush_time">当前时间：'.date("Y-m-d H:i:s",time()).'</div>';
$html .= '        </div>';
$html .= '    </div>';
$html .= '</form>';
$html .= '<table width="100%" cols="3">';

$row_col_num = 3;
function canvasCell($itemDetail,$html){
    $html .= '<div class="item_area">';
    $html .= '  <a href="https:'.$itemDetail->url.'&ali_trackid=2:mm_17948423_19174410_66870556">';
    $html .= '        <div class="item_title">'.$itemDetail->name.'</div>';
    $html .= '        <div class="item_price"><span class="span_hb">'.($itemDetail->minPrice/100).'-'.($itemDetail->maxPrice/100).'元</span>';
    $html .= '        <span class="span_price">'.$itemDetail->reminNum.'人</span> <span class="span_price1">群号:527218520</span></div>';
    $html .= '        <div class="item_img"><img src="'.$itemDetail->pic.'" height="100%" width="45%">';
    $html .= '       <img src="http://s.jiathis.com/qrcode.php?url='.urlencode('https:'.$itemDetail->url.'').'" height="100%" width="45%"></div>';
    $html .= '    </a>';
    $html .= '</div>';
    return $html;
}
$row = ceil($count/$row_col_num);

for($i = 0;$i<$row;$i++){
    $html .= '<tr>';
    for($j = 0;$j<$row_col_num;$j++) {

        $index = $i * $row_col_num + $j;
        if ($index >= $count - 1) {
            $html .= '<td width="33%"></td>';
            continue;
        };
        $html .= '<td width="33%">';
        $html = canvasCell($dataArray[$index], $html);
        $html .= '</td>';
    }
    $html .= '</tr>';
}

$html .= '</table></div>';
$html .= '<div class="bottom_line">';
$html .= '    <div class="condition_text">用户名:</div><div class="header_line_text">xxx</div>';
$html .= '    <div class="condition_text">用户组:</div><div class="header_line_text">包月vip</div>';
$html .= '    <div class="condition_text">有效时间:</div><div class="header_line_text">20161010</div>';
$html .= '    <input class="register_input" type="button" value="续费">';
$html .= '    <input class="register_input" type="button" value="退出">';
$html .= '</div>';
$html .= '</body></html>';

echo $html;